package com.javaking.clanteam.hvz.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.javaking.clanteam.hvz.EditUserActivity;
import com.javaking.clanteam.hvz.R;
import com.javaking.clanteam.hvz.sql.HVZUser;
import com.javaking.clanteam.hvz.sql.HVZUserArray;
import com.javaking.clanteam.hvz.sql.SQLRelay;

public class SelectUserActivity extends Activity {
	
	
//	private final String[] from = {"user_id","barcode",
//			"username","usertype","status",
//			"assigned_killcodes","acquired_killcodes"};
	
	private final String[] from = {"user_id","barcode",
			"username","usertype","status"};
	
	private final int[] to = {R.id.user_id,R.id.barcode,
			R.id.username,R.id.usertype,R.id.status};
	
	private ListView usersList;
	private HVZUserArray allUsers;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       allUsers = SQLRelay.readUsers(new ArrayList<NameValuePair>());
       setContentView(R.layout.select_user_screen);
       HVZUserArray users = SQLRelay.readUsers(new ArrayList<NameValuePair>());
       usersList = (ListView)findViewById(R.id.userSearch);
       
       usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    	   
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				HVZUser user = new HVZUser();
				user.setId(((TextView)view.findViewById(R.id.user_id))
						.getText().toString());
				user.setBarcode(((TextView)view.findViewById(R.id.barcode))
						.getText().toString());
				user.setUsername(((TextView)view.findViewById(R.id.username))
						.getText().toString());
				user.setUsertype(((TextView)view.findViewById(R.id.usertype))
						.getText().toString());
				user.setStatus(((TextView)view.findViewById(R.id.status))
						.getText().toString());
				
				Bundle extras = new Bundle();
				extras.putString("HVZUser_string", user.toString());
				setResult(RESULT_OK, new Intent().putExtras(extras));
				
				Intent editIntent = new Intent(SelectUserActivity.this, EditUserActivity.class);
				editIntent.putExtras(extras);
				startActivity(editIntent);
			}
		});
	       
       updateUserList(users);
       
       ((EditText)findViewById(R.id.select_user_editText))
       			.addTextChangedListener(new TextWatcher() {
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			HVZUserArray filteredUsers = filterUsers(s.toString());
			updateUserList(filteredUsers);
		}
		
		public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
		
		public void afterTextChanged(Editable s) {}
	});
   }
   
   @Override
	protected void onResume() {
		super.onResume();
		allUsers = SQLRelay.readUsers(new ArrayList<NameValuePair>());
		updateUserList(filterUsers(((EditText)findViewById(
				R.id.select_user_editText)).getText().toString()));
	}

	protected HVZUserArray filterUsers(String filterText) {
		HVZUserArray users = new HVZUserArray();
		for (int i=0; i<allUsers.length(); i++)
		{
			HVZUser user = this.allUsers.getHVZUser(i);
			if(user!=null)
			{
				try {
					boolean id = user.getId().toUpperCase().startsWith(filterText.toUpperCase());
					boolean barcode = user.getBarcode().startsWith(filterText);
					boolean username = user.getUsername().startsWith(filterText);
					boolean usertype = user.getUsertype().startsWith(filterText);
					boolean status = user.getStatus().startsWith(filterText);
					if (id||barcode||username||usertype||status)
					{
						users.put(user);
					}
				} catch (IndexOutOfBoundsException e) {}
				  catch (NullPointerException e) {}
			}
		}
		return users;
	}

	private void updateUserList(HVZUserArray users) {
		List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String,String>>();
	       for(int i = 0; i<users.length();i++)
	       {
	    	   HVZUser user = users.getHVZUser(i);
	    	   if (user==null) break;
	    	   
	    	   HashMap<String, String> map = new HashMap<String, String>();
	    	   map.put(from[0], user.getId());
	    	   map.put(from[1], user.getBarcode());
	    	   map.put(from[2], user.getUsername());
	    	   map.put(from[3], user.getUsertype());
	    	   map.put(from[4], user.getStatus());
	    	   fillMaps.add(map);
	       }
	       
	       SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
	    		   R.layout.list_item, from, to);
	       usersList.setAdapter(adapter);
	}
}
